﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq; 
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DatabaseLibrary;

namespace DatabaseTest
{
    public partial class Form1 : Form
    {
        TheatreDatabase db;
        public Form1()
        {
            InitializeComponent();
            db = new TheatreDatabase();
            Initialize();

        }
        public void Initialize()
        {
             listBox1.Items.Clear();
            listBox1.Items.AddRange(db.GetPlays().ToArray());

            listBox1.DisplayMember = "Title";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((sender as ListBox).SelectedItem == null)
            {
                return;
            }
            else
            {
                Play selectedPlay = (Play)(sender as ListBox).SelectedItem;
                textBox1.Text = selectedPlay.Description;
                List<Performance> performance = db.GetPerformancesForPlay(selectedPlay.PlayId);
                listBox2.Items.Clear();
                listBox2.Items.AddRange(performance.ToArray());
                listBox2.DisplayMember="Time";
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            {
                return;
            }
            else
            {
                Play selectedPlay = (Play)listBox1.SelectedItem;
               int performaceId =  db.AddPerformanceToPlay(selectedPlay.PlayId,new DateTime(2011,11,26,19,00,00));
               db.CreateTicketsForPerformance(performaceId);
               Initialize();
              
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Performance selectedperformance = (Performance)listBox2.SelectedItem;
            if (selectedperformance != null)
            {
                Tickets alltickets = new Tickets();
                string ticketsinfo = alltickets.GetTickets(selectedperformance.PerformanceId);
                textBox2.Text = ticketsinfo;
            }
        }

       
    }
}
